<%@ page language="java" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<input type="hidden" name="aType" id="aType" value="<s:property value="stepBrief.actionType" />" />
<input type="hidden" name="sType" id="sType" value="<s:property value="stepBrief.specialType" />" />
<input type="hidden" name="chooseId" id="chooseId" />
<!-- row -->
<div class="modal-body" style="height: 500px ">
    <div class="row">
        <article class="col-sm-12 col-md-12 col-lg-12 sortable-grid ui-sortable">
            <div class="jarviswidget well" id="wid-id-3"
                        data-widget-colorbutton="false"
                        data-widget-editbutton="false"
                        data-widget-togglebutton="false"
                        data-widget-deletebutton="false"
                        data-widget-fullscreenbutton="false"
                        data-widget-custombutton="false"
                        data-widget-sortable="false">
                 <div>
                    <div class="widget-body">
                        <a class="btn btn-default" id="btn-re-common2" href="javascript:void(0)"><i class="fa fa-lg fa-mail-reply-all"></i>返回</a>
                        <a class="btn btn-default pull-right pull-right-fix" id="btn-save-common" href="javascript:void(0);"><i class="fa fa-lg fa-download"></i> 保存</a>
                        <hr class="simple" />
                        <form id="form_stepbrief" class="smart-form form-horizontal"  action="" method="post">
                            <input type="hidden" name="parentId" id="parentId" value="<s:property value="stepBrief.processBrief.id" />"/>
                            <input type="hidden" name="keyId" id="keyId" value="<s:property value="stepBrief.id" />"/>
                            <input type="hidden" name="commonConfigId" id="commonConfigId" value="<s:property value="stepBrief.commonConfig.id" />"/>
                            <fieldset>
                                <legend>基本信息</legend>
                                <div class="form-group">
                                    <label class="col-md-2 control-label">步骤编号：</label>
                                    <div class="col-md-8">
                                        <div class="input">
                                            <input class="form-control" type="text"  name="stepNo" id="stepNo"  placeholder="请输入步骤编号"
                                                   value="<s:if test="stepBrief.stepNo==0"></s:if><s:else><s:property value="stepBrief.stepNo"/></s:else>">
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-md-2 control-label">步骤名称：</label>
                                    <div class="col-md-8">
                                        <div class="input">
                                            <input class="form-control" type="text"  name="stepName" id="stepName"  placeholder="请输入步骤名称"
                                                   value="<s:property value="stepBrief.name"/>">
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-md-2 control-label">参与者权限：</label>
                                    <div class="col-md-8">
                                        <div class="input-group" style="margin-top:5px">
                                            <label style="float:left;padding-right:10px">
                                                <input type="checkbox" id="back" name="actionType" value="1" class="checkbox">
                                                <span>允许退回</span>
                                            </label>
                                            <label  style="float:left;padding-right:10px">
                                                <input type="checkbox" id="deny" name="actionType" value="2"  class="checkbox">
                                                <span>允许否决</span>
                                            </label>
                                       </div>
                                    </div>
                                </div>
                                <div class="form-group" style="display:none" id="showBackStepNo">
                                    <label class="col-md-2 control-label">退回步骤编号：</label>
                                    <div class="col-md-8">
                                        <div class="input">
                                            <input class="form-control" type="text"  name="backStepNo" id="backStepNo"  placeholder="请输入退回步骤编号"
                                                   value="<s:if test="stepBrief.backStepNo==0"></s:if><s:else><s:property value="stepBrief.backStepNo"/></s:else>">
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-md-2 control-label">是否会签</label>
                                    <div class="col-md-8">
                                        <div class="input-group" style="margin-top:5px">
                                            <label style="float:left;padding-right:10px">
                                                <input type="radio" name="jointlySing" value="1" class="radiobox" <s:if test="stepBrief.jointlySing==1">checked</s:if> >
                                                <span>是</span>
                                            </label>
                                            <label style="float:left;padding-right:10px">
                                                <input type="radio" name="jointlySing" value="0" class="radiobox" <s:if test="stepBrief.jointlySing==0">checked</s:if> >
                                                <span>否</span>
                                            </label>
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-md-2 control-label">路由类型</label>
                                    <div class="col-md-8">
                                        <div class="input-group" style="margin-top:5px">
                                            <label style="float:left;padding-right:10px">
                                                <input type="radio" name="routeType" value="1" class="radiobox" <s:if test="stepBrief.routeType != 2">checked</s:if> />
                                                <span>一般路由</span>
                                            </label>
                                            <label  style="float:left;padding-right:10px">
                                                <input type="radio" name="routeType" value="2" class="radiobox" <s:if test="stepBrief.routeType == 2">checked</s:if> />
                                                <span>条件路由</span>
                                            </label>
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group" id="div_nextStepNo" <s:if test="stepBrief.routeType == 2">style="display:none"</s:if>>
                                    <label class="col-md-2 control-label">下一步步骤编号(0代表归档)：</label>
                                    <div class="col-md-8">
                                        <div class="input">
                                            <input  class="form-control" type="text" name="nextStepNo" id="nextStepNo"  placeholder="请输入下一步步骤编号"
                                                    value="<s:property value="stepBrief.nextStepNo"/>">
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group" id="ajax_stepCon_list_row" <s:if test="stepBrief.routeType != 2">style="display:none"</s:if>>
                                    <label class="col-md-2 control-label">
                                        <a id="ajax_stepCon_btn_add"  class="btn btn-default " data-toggle="modal"><i class="fa fa-lg fa-plus"></i> 新建条件路由</a>
                                    </label>
                                    <div class="col-md-8">
                                        <table id="ajax_stepCon_table" class="table table-striped table-bordered table-hover">
                                        </table>
                                        <div id="ajax_stepCon_list_page">
                                        </div>
                                    </div>
                                </div>
                            </fieldset>
                            <fieldset>
                                <legend>参与者信息</legend>
                                <div class="form-group">
                                    <label class="col-md-2 control-label">所有人： </label>
                                    <div class="col-md-8">
                                        <div class="checkbox">
                                            <label>
                                                <input type="checkbox" value="1" name="allmans" <s:if test='stepBrief.commonConfig.allmans==true'>checked</s:if> class="checkbox style-0">
                                                <span style="margin-left:-45px;margin-top:-14px"></span>
                                            </label>
                                        </div>
                                    </div>
                                </div>

                                <div class="form-group">
                                    <label class="col-md-2 control-label">部门： </label>
                                    <div class="col-md-8">
                                        <div class="input state-disabled" style="display:table">
                                            <input class="form-control" disabled type="text"  id="departName" value="<s:iterator id="list" value="stepBrief.commonConfig.departmentSet"><s:if test="#list.state.toString() == 'Enable'"><s:property value="#list.name"/>,</s:if></s:iterator>"/>
                                            <input type="hidden" type="text" name="departId" id="departId"
                                            value="<s:iterator id="list" value="stepBrief.commonConfig.departmentSet"><s:if test="#list.state.toString() == 'Enable'"><s:property value="#list.id"/>,</s:if></s:iterator>"/>
                                            <div class="input-group-btn">
                                                <button class="btn" id="btn-choose-depart" type="button" style="font-size: 12px;">选择部门</button>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-md-2 control-label">岗位： </label>
                                    <div class="col-md-8">
                                        <div class="input state-disabled" style="display:table">
                                            <input class="form-control" disabled type="text" id="postName"
                                                   value="<s:iterator id="list" value="stepBrief.commonConfig.postSet"><s:if test="#list.state.toString() == 'Enable'"><s:property value="#list.name"/>,</s:if></s:iterator>"/>
                                            <input type="hidden" type="text" name="postId" id="postId"
                                                    value="<s:iterator id="list" value="stepBrief.commonConfig.postSet"><s:if test="#list.state.toString() == 'Enable'"><s:property value="#list.id"/>,</s:if></s:iterator>"/>
                                            <div class="input-group-btn">
                                                <button class="btn" id="btn-choose-post" type="button" style="font-size: 12px;">选择岗位</button>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-md-2 control-label">职权： </label>
                                    <div class="col-md-8">
                                        <div class="input state-disabled" style="display:table">
                                            <input class="form-control" disabled type="text"  id="powerName"
                                                   value="<s:iterator id="list" value="stepBrief.commonConfig.powerSet"><s:if test="#list.state.toString() == 'Enable'"><s:property value="#list.name"/>,</s:if></s:iterator>"/>
                                            <input type="hidden" type="text" name="powerId" id="powerId"
                                                   value="<s:iterator id="list" value="stepBrief.commonConfig.powerSet"><s:if test="#list.state.toString() == 'Enable'"><s:property value="#list.id"/>,</s:if></s:iterator>"/>
                                            <div class="input-group-btn">
                                                <button class="btn" id="btn-choose-power" type="button" style="font-size: 12px;">选择职权</button>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-md-2 control-label">群组： </label>
                                    <div class="col-md-8">
                                        <div class="input state-disabled" style="display:table">
                                            <input class="form-control" disabled type="text" id="roleName" value="<s:iterator id="list" value="stepBrief.commonConfig.roleSet"><s:if test="#list.state.toString() == 'Enable'"><s:property value="#list.name"/>,</s:if></s:iterator>"/>
                                            <input type="hidden" type="text" name="roleId" id="roleId" value="<s:iterator id="list" value="stepBrief.commonConfig.roleSet"><s:if test="#list.state.toString() == 'Enable'"><s:property value="#list.id"/>,</s:if></s:iterator>"/>
                                            <div class="input-group-btn">
                                                <button class="btn" id="btn-choose-role"  type="button" style="font-size: 12px;">选择群组</button>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-md-2 control-label">域名：</label>
                                    <div class="col-md-8">
                                        <select class="form-control" name="variableName" id="variableName">
                                            <option value="">--请选择--</option>
                                            <s:iterator value="fieldNames" id="arr" status="st" >
                                                <option  value="<s:property value="#arr"/>"  <s:if test='#arr==stepBrief.commonConfig.variable'>selected</s:if>><s:property value="#arr"/></option>
                                            </s:iterator>
                                        </select>
                                    </div>
                                </div>
                            </fieldset>
                            <fieldset>
                                <legend>特殊设定</legend>
                                <div class="form-group ">
                                    <label class="col-md-2 control-label"></label>
                                    <div class="col-md-8">
                                        <div class="checkbox">
                                            <label>
                                                <input type="checkbox" id="power" name="specialType" value="1"  class="checkbox style-0">
                                                <span>上一步操作用户职权上级</span>
                                            </label>
                                        </div>
                                        <div class="checkbox">
                                            <label>
                                                <input type="checkbox" id="header" name="specialType" value="2" class="checkbox style-0">
                                                <span>上一步操作用户所属部门负责人</span>
                                            </label>
                                        </div>
                                        <div class="checkbox">
                                            <label>
                                                <input type="checkbox" id="upheader" name="specialType" value="3" class="checkbox style-0">
                                                <span>上一步操作用户所属部门的上级部门负责人</span>
                                            </label>
                                        </div>
                                    </div>
                                </div>
                            </fieldset>
                        </form>
                    </div>
                 </div>
            </div>
        </article>
    </div>
</div>
<%--
<div class="modal-footer">
	<a href="javascript:void(0);" class="btn btn-primary" id="a-dialog-ok">确定</a>
	<button class="btn" data-dismiss="modal" aria-hidden="true">取消</button>
</div>
--%>
<script type="text/javascript">
pageSetUp();

$(function(){
   // if($(':checked[name="routeType"]').val() == 2){
   // jQuery("#ajax_stepCon_table").jqGrid("clearGridData");
   // $("#ajax_stepCon_table").jqGrid('GridDestroy');
    load_stepCon_jqGrid();
   // }
    var aType = $('#aType').val();
    var sType = $('#sType').val();
    $("input[name='actionType']").each(function(i,v){
        var optVal = $(v).val();
        if(aType != "" && aType.indexOf(optVal) != -1){
            $(v).prop("checked",true);
        }
    });
    $(':checked[name="actionType"]').each(function(){
        if($(this).val() == "1"){
            $('#showBackStepNo').show();
            return false;
        }
    });
    $("input[name='specialType']").each(function(i,v){
        var optVal = $(v).val();
        if(sType != "" && sType.indexOf(optVal) != -1){
            $(v).prop("checked",true);
        }
    });

    $(':radio[name="routeType"]').click(function(){
        if($(this).val()==2){
            $('#div_nextStepNo').hide();
            $('#ajax_stepCon_list_row').show();
        }else{
            $('#div_nextStepNo').show();
            $('#ajax_stepCon_list_row').hide();
        }
    });

    $(":input[name='actionType']").click(function(){
        if($(this).val() == "1"){
            if($(this).is(":checked")){
                $('#showBackStepNo').show();
            }else{
                $('#showBackStepNo').hide();
            }
        }
    })
    //返回视图
    $("#btn-re-common2").click(function(){
        loadURL("../com/ajax!stepBrief.action",$('div#s2'));
    });

    //保存步骤简要表
    $("#btn-save-common").click(function(){
        var $valid = $("#form_stepbrief").valid();
        if(!$valid) return false;
        form_save("form_stepbrief","<%=path%>/com/ajax-stepbrief!save.action");
        loadURL("../com/ajax!stepBrief.action",$('div#s2'));
    });

    //新建条件路由
    $('#ajax_stepCon_btn_add').click(function(){
        if($('#form_stepbrief #keyId').val()==""){
            alert("请先保存步骤简要表");
            return false;
        }
        gDialog.fCreate({
            title:"条件路由",
            url:"../com/ajax-stepcon!input.action?parentId="+$('#form_stepbrief #keyId').val(),
            width:600
        }).show();
    })
});

function load_stepCon_jqGrid(){
    jQuery("#ajax_stepCon_table").jqGrid({
        url:'../com/ajax-stepcon!list.action?parentId='+$('#form_stepbrief #keyId').val(),
        datatype: "json",
        colNames:['条件名称',"条件描述","条件公式","路由步骤","操作","id"],
        colModel:[
            {name:'name',index:'name',sortable:false,search:false,width:120},
            {name:'content',index:'content',sortable:false,search:false,width:200},
            {name:'formula',index:'formula',sortable:false,search:false,width:200},
            {name:'stepNo',index:'stepNo',sortable:false,search:false,width:70},
            {name:'act',index:'act', width:130,sortable:false,search:false,fixed:true},
            {name:'id',index:'id',search:false,hidden:true,key:true}
        ],
        rowNum : 10,
        rowList:[10,20,30],
        pager : '#ajax_stepCon_list_page',
        sortname : '',
        sortorder : "",
        gridComplete:function(){
            var ids=$("#ajax_stepCon_table").jqGrid('getDataIDs');
            for(var i=0;i<ids.length;i++){
                var cl=ids[i];
                var rowData = $("#ajax_stepCon_table").jqGrid("getRowData",cl);
                var de="<a class='btn btn-default' data-toggle='modal' flag='edit' id='e"+cl+"' key='"+cl+"'><i class='fa fa-eye'></i>编辑</a>"+" ";
                var dt="<a class='btn btn-default' data-toggle='modal' flag='del' id='d"+cl+"' key='"+cl+"'><i class='fa fa-eye'></i>删除</a>"+" ";
                jQuery("#ajax_stepCon_table").jqGrid('setRowData',ids[i],{act:de+dt});
                //编辑
                $('#e'+cl).click(function(){
                    var id = $(this).attr('key');
                    gDialog.fCreate({
                        title:"条件路由",
                        url:"../com/ajax-stepcon!input.action?keyId="+id,
                        width:600
                    }).show();
                    return false;
                });
                //删除
                $('#d'+cl).click(function(){
                    var id = $(this).attr('key');
                    $.tzDialog({title:"提示",content:"确认要删除吗?",callback:function(){
                        var vActionUrl = "../com/ajax-stepcon!delete.action";
                        var data={keyId:id};
                        ajax_action(vActionUrl,data,{},function(pdata){
                            _show(pdata);
                            jQuery("#ajax_stepCon_table").jqGrid().setGridParam({datatype:'json'}).trigger("reloadGrid");
                        });
                    }});
                    return false;
                });
            }
            $(".ui-jqgrid-bdiv").css("overflow-x","hidden");
            jqGridStyle();
        },
        onSelectRow: function (rowId, status, e) {

            var rowId = $("#ajax_stepCon_table").jqGrid('getGridParam','selrow');
            var rowDatas = $("#ajax_stepCon_table").jqGrid('getRowData', rowId);
            //$('#chooseId').val(rowId);
        },
        jsonReader: {
            root: "dataRows",
            page: "page",
            total: "total",
            records: "records",
            repeatitems : false
        },
        caption : "<i class='fa fa-arrow-circle-right'></i> 步骤条件",
       // multiselect : true,
        rownumbers:true,
        gridview:true,
        shrinkToFit:true,
        viewrecords: true,
        autowidth: true,
        height:'auto',
        forceFit:true,
        loadComplete: function() {
        }
    });
    $(window).on('resize.jqGrid', function() {
        jQuery("#ajax_stepCon_table").jqGrid('setGridWidth', $("#ajax_stepCon_list_row").width());
    })
    jQuery("#ajax_stepCon_table").jqGrid('filterToolbar',{searchOperators:false,stringResult:true});

    jQuery("#ajax_stepCon_table").jqGrid('navGrid', "#ajax_stepCon_list_page", {
        edit : false,
        add : false,
        del : false,
        search:false
    });
};

//校验
$("#form_stepbrief").validate({
    rules : {
        stepNo : {
            required : true,
            digits:true
        },
        stepName:{
            required : true
        },
        actionType:{
           // required : true
        },
        backStepNo:{
            required: function(){
                if($('#showBackStepNo').is(":visible")){
                    return true;
                }else{
                    return false;
                }
            },
            digits: true
        },
        jointlySing:{
            required:true
        },
        routeType:{
            required:true
        },
        nextStepNo:{
            required: function(){
                if($(':checked[name="routeType"]').val() =="1"){
                    return true;
                }else{
                    return false;
                }
            },
            digits:true
        }
    },
    messages : {
        stepNo : {
            required : "请输入步骤编号",
            digits: "步骤编号必须为数字"
        },
        stepName:{
            required :"请输入步骤名称"
        },
        actionType:{
            required : "请选择参与者权限"
        },
        backStepNo:{
            required : "请输入退回步骤编号",
            digits: "退回步骤编号必须为数字"
        },
        jointlySing:{
            required:"请选择是否会签"
        },
        routeType:{
            required:"请选择路由类型"
        },
        nextStepNo:{
            required :"请输入下一步步骤编号",
            digits:"下一步步骤编号必须为数字"
        }
    },
    errorPlacement : function(error, element) {
        if(element.is(":radio")){
            element.parents("div.input-group").append(error);
        }else {
            error.insertAfter(element.parent());
        }
    }
});
$("#btn-choose-role").unbind("click").bind("click",function(){
      gDialog.fCreate({
           title:"请选择群组",
           url:"../com/ajax-dialog!role.action",
           width:340
        }).show();
});
$("#btn-choose-depart").unbind("click").bind("click",function(){
    gDialog.fCreate({
               title:"请选择部门",
               url:"../com/ajax-dialog!mdepart.action",
               width:500
            }).show();
});
$("#btn-choose-post").unbind("click").bind("click",function(){
     gDialog.fCreate({
           title:"请选择岗位",
           url:"../com/ajax-dialog!mpost.action",
           width:340
        }).show();
});
$("#btn-choose-power").unbind("click").bind("click",function(){
    gDialog.fCreate({
           title:"请选择职权",
           url:"../com/ajax-dialog!power.action",
           width:340
        }).show();
});


/*
$("#btn-choose-field").unbind("click").bind("click",function(){
    gDialog.fCreate({
        title:"请选择域",
        url:"../com/ajax-dialog!field.action?keyId="+"<s:property value="stepBrief.id"/>",
        width:340
    }).show();
});
$("#a-dialog-ok").unbind("click").bind("click",function(){
    alert('do')
        var actionUrl = "../com/ajax-stepbrief!save.action";
        form_save("form_stepbrief",actionUrl);
        loadURL("../com/ajax!stepBrief.action",$('div#s2'));
    });*/
</script>